<template>
    <div class="session-container content-wrapper">
        <div class="filter-container">
            <el-form :model="listQuery" :inline="true" ref="filterForm" style="display: inline-block">
                <el-form-item prop="realName" class="filter-item">
                    <el-input v-model="listQuery.realName" placeholder="姓名" style="width: 200px;"></el-input>
                </el-form-item>
                <el-form-item prop="account" class="filter-item">
                    <el-input v-model="listQuery.account" placeholder="账号" style="width: 200px;"></el-input>
                </el-form-item>
                <el-form-item prop="token" class="filter-item">
                    <el-input v-model="listQuery.token" placeholder="令牌" style="width: 200px;"></el-input>
                </el-form-item>

                <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
                <el-button class="filter-item" type="primary" icon="mars-reset" @click="handleResetFilter">重置</el-button>
                <el-button class="filter-item" type="success" icon="mars-refresh" @click="handleReload">刷新</el-button>
            </el-form>
        </div>

        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">

            <el-table-column label="ID" width="65" align="center">
                <template slot-scope="scope"> {{scope.row.id}} </template>
            </el-table-column>

            <el-table-column label="名称" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.realName}} </template>
            </el-table-column>

            <el-table-column label="账号" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.account}} </template>
            </el-table-column>

            <el-table-column label="手机号" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.mobile}} </template>
            </el-table-column>

            <el-table-column label="邮箱" width="200px" align="center" show-overflow-tooltip>
                <template slot-scope="scope"> {{scope.row.email}} </template>
            </el-table-column>

            <el-table-column label="IP" width="100px" align="center">
                <template slot-scope="scope"> {{scope.row.ip}} </template>
            </el-table-column>

            <el-table-column label="令牌" width="280px" align="center">
                <template slot-scope="scope"> {{scope.row.token}} </template>
            </el-table-column>

            <el-table-column label="过期时间(s)" width="100px" align="center">
                <template slot-scope="scope">{{scope.row.expires}} </template>
            </el-table-column>

            <el-table-column label="标识" width="400px" align="center" show-overflow-tooltip>
                <template slot-scope="scope"> {{scope.row.identify}} </template>
            </el-table-column>

            <el-table-column label="操作" fixed="right" width="180px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="danger" icon="delete" @click="handleLogout(scope.row)">踢出登录</el-button>
                </template>
            </el-table-column>

        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import { getOnlineUser, deleteOnlineUser } from '@/api/login'
    export default {
        name: 'session',
        data() {
            return {
                list: null,
                total: null,
                listLoading: true,
                listQuery: {
                    page: 1,
                    rows: 10,
                    realName: undefined,
                    account: undefined,
                    token: undefined
                }
            }
        },
        created() {
            this.getList()
        },
        methods: {
            getList() {
                this.listLoading = true
                getOnlineUser({'token': this.$store.state.user.token}).then(response => {
                    this.list = response.data
                    this.listLoading = false
                }).catch((error)=>{
                    console.log(error)
                    this.listLoading = false
                })
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            handleResetFilter() {
                this.$refs.filterForm.resetFields()
                this.getList()
            },
            handleReload() {
                this.$refs.filterForm.resetFields()
                this.getList()
            },
            handleLogout(row) {
                if(row.token === this.$store.state.user.token) {
                    this.$notify({
                        title: '提示', message: '不能踢出自己',
                        type: 'warning', duration: 2000
                    })
                    return ;
                }
                deleteOnlineUser(row.token).then(()=>{
                    this.getList()
                })

            }
        }
    }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>


</style>